function ScrollBar(root){
    this.root = root;
    this.x = 0;
    this.xstart = 0;
    this.tx = 0;
    this.ty = 0;
    this.touched = false;
    this.inner = root.find(".ncf-scroll-content");
    this.SliderWidth = this.root.innerWidth();
    this.initEvents();
    this.move(0);
}
ScrollBar.prototype = {
    constructor: ScrollBar,
    initEvents: function(){
        this.root.on("touchstart",(e)=>{
            this.touch=true;
            this.xstart = this.tx = e.targetTouches[0].screenX;
            this.ty = e.targetTouches[0].screenY;
        });
        this.root.on("touchend",function(e){
                this.touch=false;
                this.move(1,true);
            }.bind(this));
        
        this.root.on("touchmove",(function(e){
            this.move((e.targetTouches[0].screenX-this.tx)*1.3);
        
            if(Math.abs(this.xstart-e.targetTouches[0].screenX)>20||Math.abs(e.targetTouches[0].screenX-this.tx)*3>Math.abs(e.targetTouches[0].screenY-this.ty)){
                e.preventDefault();
                e.stopPropagation();
                e.stopImmediatePropagation();
            }
            this.tx = e.targetTouches[0].screenX;
            this.ty = e.targetTouches[0].screenY;
        }.bind(this)));
        
        
        
        $(window).resize(function(){
            if(this.SliderWidth != this.root.innerWidth()){
                this.x=Math.round(this.x* this.root.innerWidth()/this.SliderWidth);
        
                this.move(0,true);
            }
        }.bind(this));

    },
    move: function(dx,animated=false){

        min = -(this.inner.innerWidth()-innerWidth);
        if(dx+this.x > Math.min(min,0) && dx+this.x<Math.max(0,min)){
            this.x = dx + this.x;
        }
        if(animated){
            this.inner.css("transition","transform 2s ease");
        }else{
            this.inner.css("transition","none");
        }
        this.inner.css("transform","translateX("+this.x+"px)");
    }

}





$(document).ready(()=>{
    $(".ncf-scrollbar").each((i,o)=> new ScrollBar($(o)));
});